<template>
  <el-drawer
    v-model="drawerVisible"
    direction="rtl"
    size="600px"
    with-header
    :destroy-on-close="false"
    @close="handleClose"
  >
    <template #header>
      <div class="drawer-custom-header">
        <h3>患者详情</h3>
      </div>
    </template>

    <div class="drawer-content-wrapper" v-loading="loading">
      <!-- 顶部患者信息卡片 -->
      <div class="patient-profile-card">
        <div class="profile-header">
          <el-avatar
            :size="80"
            :src="patientInfo.avatar || defaultAvatar"
            class="profile-avatar"
          />
          <div class="profile-info">
            <h2 class="patient-name">{{ patientInfo.patient_name || '-' }}</h2>
            <div class="patient-meta">
              <el-tag :type="getStatusType(patientInfo.latest_visit_status)" size="small">
                {{ getStatusText(patientInfo.latest_visit_status) }}
              </el-tag>
              <span class="patient-id">{{ patientInfo.id_number || '无身份证号' }}</span>
            </div>
            <div class="patient-basic">
              <span>{{ patientInfo.gender || '-' }}</span>
              <span class="divider">|</span>
              <span>{{ patientInfo.age || '-' }}岁</span>
              <span class="divider">|</span>
              <span>{{ patientInfo.blood_type || '未知血型' }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 详细信息区域 -->
      <div class="detail-sections">
        <!-- 基本信息 -->
        <div class="detail-section">
          <div class="section-header">
            <el-icon><User /></el-icon>
            <span>基本信息</span>
          </div>
          <div class="section-content">
            <div class="info-grid">
              <div class="info-item">
                <label>性别</label>
                <span>{{ patientInfo.gender || '-' }}</span>
              </div>
              <div class="info-item">
                <label>年龄</label>
                <span>{{ patientInfo.age || '-' }}岁</span>
              </div>
              <div class="info-item">
                <label>手机号</label>
                <span class="highlight">{{ patientInfo.phone_number || '-' }}</span>
              </div>
              <div class="info-item">
                <label>身份证号</label>
                <span>{{ patientInfo.id_number || '-' }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 健康信息 -->
        <div class="detail-section">
          <div class="section-header">
            <el-icon><Monitor /></el-icon>
            <span>健康信息</span>
          </div>
          <div class="section-content">
            <div class="info-item-full">
              <label>过敏史</label>
              <div class="info-content">
                {{ patientInfo.allergy_history || '无' }}
              </div>
            </div>
            <div class="info-item-full">
              <label>慢性疾病</label>
              <div class="info-content">
                <div class="disease-tags">
                  <el-tag
                    v-if="patientInfo.chronic_diseases"
                    type="danger"
                    size="small"
                    class="disease-tag"
                  >
                    {{ patientInfo.chronic_diseases }}
                  </el-tag>
                  <span v-else class="no-data-text">无</span>
                </div>
              </div>
            </div>
            <div class="info-item-full">
              <label>家族病史</label>
              <div class="info-content">
                {{ patientInfo.family_history || '无' }}
              </div>
            </div>
          </div>
        </div>

        <!-- 最近就诊信息 -->
        <div class="detail-section">
          <div class="section-header">
            <el-icon><Calendar /></el-icon>
            <span>最近就诊</span>
          </div>
          <div class="section-content">
            <div class="recent-visit-card" v-if="patientInfo.latest_visit_time">
              <div class="visit-header">
                <div class="visit-date">{{ formatDate(patientInfo.latest_visit_time) }}</div>
                <el-tag type="primary" size="small">最近就诊</el-tag>
              </div>
              <div class="visit-diagnosis">
                {{ patientInfo.latest_diagnosis || '暂无诊断信息' }}
              </div>
              <div class="visit-meta">
                <div class="visit-doctor">
                  <el-icon><User /></el-icon>
                  接诊医生：{{ patientInfo.latest_doctor || '未知' }}
                </div>
                <div class="visit-department" v-if="patientInfo.latest_department">
                  <el-icon><OfficeBuilding /></el-icon>
                  科室：{{ patientInfo.latest_department }}
                </div>
              </div>
            </div>
            <div v-else class="no-visit-data">
              <el-icon><DocumentRemove /></el-icon>
              <div>暂无就诊记录</div>
            </div>
          </div>
        </div>

        <!-- 统计信息 -->
        <div class="detail-section">
          <div class="section-header">
            <el-icon><DataAnalysis /></el-icon>
            <span>统计信息</span>
          </div>
          <div class="section-content">
            <div class="stats-grid">
              <div class="stat-item">
                <div class="stat-value">{{ patientInfo.total_visits || 0 }}</div>
                <div class="stat-label">总就诊次数</div>
              </div>
              <div class="stat-item">
                <div class="stat-value">{{ patientInfo.record_count || 0 }}</div>
                <div class="stat-label">完成病历</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="drawer-footer">
        <el-button @click="drawerVisible = false">关闭</el-button>
        <el-button
          type="primary"
          @click="handleViewRecords"
          :disabled="!patientInfo || !patientInfo.patient_id"
        >
          <el-icon><Document /></el-icon>
          查看完整病历
        </el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  patientId: {
    type: Number,
    default: null
  },
  patientInfo: { //患者信息对象
    type: Object,
    default: null
  }
})

const emit = defineEmits(['update:modelValue', 'diagnosisStarted']);
// 默认头像读取assets目录下的图片
const defaultAvatar = require("@/assets/img/default-avatar.jpeg");

// 响应式数据
const drawerVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const loading = ref(false)
// const patientInfo = ref({})

// 监听患者ID变化
watch(() => props.patientId, (newId) => {
  if (newId && drawerVisible.value) {
    loadPatientDetail()
  }
})

// 监听抽屉显示状态
watch(() => props.modelValue, (newValue) => {
  if (newValue) {
    // 抽屉打开时，确保使用最新的patientInfo
    // 不需要重新加载，因为patientInfo已经从父组件传递过来
  } else {
    // 抽屉关闭时，可以重置内部状态（如果需要）
  }
})

// 添加对patientInfo的直接监听，确保数据更新时视图也更新
watch(() => props.patientInfo, (newInfo) => {
  // 当patientInfo prop变化时，确保使用最新数据
}, { immediate: true, deep: true })

// 方法
const loadPatientDetail = async () => {
  if (!props.patientId) return
  
  loading.value = true
  try {
    // TODO: 调用API获取患者详情
    // const response = await patientApi.getDetail(props.patientId)
    // patientInfo.value = response.data
    
    
    
  } catch (error) {
    console.error('获取患者详情失败:', error)
    ElMessage.error('获取患者详情失败')
  } finally {
    loading.value = false
  }
}

const handleClose = () => {
}

const handleViewRecords = () => {
  // 关闭当前抽屉
  drawerVisible.value = false;
  // 通知父组件打开病历总览
  emit('viewRecords', props.patientId);
}


const getStatusType = (status) => {
  const typeMap = {
    waiting: 'warning',
    in_consultation: 'primary',
    completed: 'success'
  }
  return typeMap[status] || 'info'
}

const getStatusText = (status) => {
  const textMap = {
    waiting: '待就诊',
    in_consultation: '就诊中',
    completed: '已完成'
  }
  return textMap[status] || status
}

const formatDate = (dateString) => {
  if (!dateString) return '-'
  // 简单格式化，实际项目中可以使用dayjs
  return dateString.replace('T', ' ').substring(0, 16)
}
</script>

<style lang="scss" scoped>
// 抽屉自定义头部
.drawer-custom-header {
  h3 {
    margin: 0;
    color: #303133;
    font-size: 18px;
    font-weight: 600;
  }
}

.drawer-content-wrapper {
  padding: 0 20px;
  height: 100%;
  overflow-y: auto;
}

// 患者信息卡片 - 医疗风格渐变
.patient-profile-card {
  background: linear-gradient(135deg, #67c23a 0%, #85ce61 100%);
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 24px;
  color: white;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -50%;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 50%;
  }
}

.profile-header {
  display: flex;
  align-items: center;
  gap: 16px;
  position: relative;
  z-index: 1;
}

.profile-avatar {
  border: 3px solid rgba(255, 255, 255, 0.2);
}

.profile-info {
  flex: 1;
  
  .patient-name {
    margin: 0 0 8px 0;
    font-size: 24px;
    font-weight: 600;
    color: white;
  }
  
  .patient-meta {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
    
    .patient-id {
      font-size: 14px;
      opacity: 0.9;
    }
  }
  
  .patient-basic {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    opacity: 0.9;
    
    .divider {
      opacity: 0.6;
    }
  }
}

// 详细信息区域
.detail-sections {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.detail-section {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  border: 1px solid #f0f0f0;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f5f5f5;
  
  .el-icon {
    color: #67c23a;
    font-size: 16px;
  }
  
  span {
    font-size: 16px;
    font-weight: 600;
    color: #303133;
  }
}

.section-content {
  .info-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
  }
  
  .info-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
    
    label {
      font-size: 13px;
      color: #909399;
      font-weight: 500;
    }
    
    span {
      font-size: 14px;
      color: #303133;
      font-weight: 500;
      
      &.highlight {
        color: #67c23a;
        font-weight: 600;
      }
    }
  }
  
  .info-item-full {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 16px;
    
    &:last-child {
      margin-bottom: 0;
    }
    
    label {
      font-size: 13px;
      color: #909399;
      font-weight: 500;
    }
    
    .info-content {
      font-size: 14px;
      color: #303133;
      line-height: 1.5;
      
      .no-data-text {
        color: #909399;
        font-style: italic;
      }
    }
  }
}

// 疾病标签样式
.disease-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.disease-tag {
  margin-right: 8px;
  margin-bottom: 4px;
}

// 最近就诊卡片
.recent-visit-card {
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #67c23a;
  
  .visit-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
    
    .visit-date {
      font-weight: 600;
      color: #303133;
      font-size: 14px;
    }
  }
  
  .visit-diagnosis {
    color: #67c23a;
    font-weight: 500;
    margin-bottom: 12px;
    line-height: 1.4;
  }
  
  .visit-meta {
    display: flex;
    flex-direction: column;
    gap: 6px;
    
    .visit-doctor,
    .visit-department {
      display: flex;
      align-items: center;
      gap: 6px;
      font-size: 12px;
      color: #909399;
      
      .el-icon {
        font-size: 12px;
      }
    }
  }
}

.no-visit-data {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 40px 20px;
  color: #909399;
  
  .el-icon {
    font-size: 48px;
    opacity: 0.5;
  }
  
  div {
    font-size: 14px;
  }
}

// 统计信息样式
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.stat-item {
  text-align: center;
  padding: 16px;
  background: linear-gradient(135deg, #f0f9ff 0%, #e6f7ff 100%);
  border-radius: 8px;
  border: 1px solid #e6f7ff;
  
  .stat-value {
    font-size: 28px;
    font-weight: 700;
    color: #67c23a;
    margin-bottom: 4px;
  }
  
  .stat-label {
    font-size: 12px;
    color: #909399;
    font-weight: 500;
  }
}

// 抽屉底部
.drawer-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 20px;
  border-top: 1px solid #f0f0f0;
}

// 暗色主题适配
@media (prefers-color-scheme: dark) {
  .drawer-custom-header h3 {
    color: #e0e0e0;
  }
  
  .detail-section {
    background: #2d2d2d;
    border-color: #3d3d3d;
  }
  
  .section-header {
    border-bottom-color: #3d3d3d;
    
    span {
      color: #e0e0e0;
    }
  }
  
  .info-item label {
    color: #a0a0a0;
  }
  
  .info-item span {
    color: #e0e0e0;
  }
  
  .info-content {
    color: #e0e0e0;
  }
  
  .recent-visit-card {
    background: #3d3d3d;
    border-left-color: #67c23a;
  }
  
  .stats-grid .stat-item {
    background: linear-gradient(135deg, #2d3d4d 0%, #3d4d5d 100%);
    border-color: #3d4d5d;
  }
}

// 响应式设计
@media (max-width: 768px) {
  .drawer-content-wrapper {
    padding: 0 16px;
  }
  
  .profile-header {
    flex-direction: column;
    text-align: center;
  }
  
  .patient-meta {
    justify-content: center;
    flex-wrap: wrap;
  }
  
  .section-content .info-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  
  .detail-section {
    padding: 16px;
  }
}
</style>